home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Devices / ADB Key Spy 1.0.1b3 / ADBKS Demo.c < prev    next >
Encoding:
Text File  |  1997-03-07  |  2.2 KB  |  100 lines  |  [TEXT/R*ch]

  1.     //
  2.     //    from 'ADB Key Spy' • Pete Gontier • gurgle@apple.com
  3.     //    Macintosh Developer Technical Support
  4.     //    © 1995,1996 Apple Computer, Inc.
  5.     //
  6.     //    Changes:
  7.     //
  8.     //        when        who        what
  9.     //        --------------------------------------------------------------
  10.     //        01/11/96    PG        rewrite for extension-based ADBKS
  11.     //
  12.  
  13. #ifndef __DIALOGS__
  14. #    include <Dialogs.h>
  15. #endif
  16.  
  17. #ifndef __FONTS__
  18. #    include <Fonts.h>
  19. #endif
  20.  
  21. #ifndef __DEVICES__
  22. #    include <Devices.h>
  23. #endif
  24.  
  25. #include "ADBKS csCodes.h"
  26.  
  27. static pascal OSErr IsKeyDownAnywhere (unsigned char keyCode, Boolean *isDown)
  28. {
  29.     //
  30.     //    You need not open and close the driver every time; in fact,
  31.     //    it will be significantly slower to do so. But I do it here
  32.     //    in order to demonstrate the entire sequence.
  33.     //
  34.  
  35.     short drvrRefNum;
  36.     OSErr err = OpenDriver ("\p.adb_key_spy", &drvrRefNum);
  37.  
  38.     if (!err)
  39.     {
  40.         OSErr err2 = noErr;
  41.  
  42.         ParamBlockRec pbr;
  43.         unsigned char *csParam = (unsigned char *) pbr.cntrlParam.csParam;
  44.  
  45.         pbr.cntrlParam.ioCompletion        = nil;
  46.         pbr.cntrlParam.ioCRefNum        = drvrRefNum;
  47.         pbr.cntrlParam.csCode            = kStatusCode_IsKeyDownAnywhere;
  48.  
  49.         csParam [0] = keyCode;
  50.  
  51.         //
  52.         //    Note you cannot merely call 'Status' because it is glue
  53.         //    wrapped around 'PBStatus' which merely calls 'BlockMove'
  54.         //    to copy 'pbr.cntrlParam.csParam' into 'csParam' *after*
  55.         //    the call to 'PBStatus'. [Disassemble it for more info.]
  56.         //
  57.  
  58.         if (!(err = PBStatusSync (&pbr)))
  59.             *isDown = csParam [0];
  60.  
  61.         err2 = CloseDriver (drvrRefNum);
  62.         if (!err) err = err2;
  63.     }
  64.  
  65.     return err;
  66. }
  67.  
  68. void main (void)
  69. {
  70.     EventRecord        event;
  71.     OSErr                    err;
  72.     Boolean                isDown;
  73.  
  74.     InitGraf (&(qd.thePort));
  75.     InitFonts ( );
  76.     InitWindows ( );
  77.     InitMenus ( );
  78.     TEInit ( );
  79.     InitDialogs (nil);
  80.  
  81.     //
  82.     //    This is a more interesting sequence than one might guess.
  83.     //    Since this package performs raw-to-virtual key code translation,
  84.     //    it's non-trivial to pass the key code from an event record to
  85.     //    IsKeyDownAnywhere.
  86.     //
  87.  
  88.     while (!WaitNextEvent (keyDownMask,&event,0xFFFFFFFF,nil))
  89.         ;
  90.  
  91.     err = IsKeyDownAnywhere ((event.message & keyCodeMask) >> 8, &isDown);
  92.  
  93.     if (err)
  94.         DebugStr ("\pWhoa! IsKeyDownAnywhere returned an error!");
  95.     else if (!isDown)
  96.         DebugStr ("\pWhoa! A keyDown event was posted for a key that isn't down!");
  97.     else
  98.         DebugStr ("\pEverything is OK.");
  99. }
  100.